<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class GoodsOrderDetail extends Model
{
    use HasDateTimeFormatter;

    protected $fillable = [
        'order_id',
        'order_sn',
        'user_id',
        'category_id',
        'goods_id',
        'name',
        'img',
        'number',
        'original_price',
        'price',
        'profit',
        'total_original_price',
        'total_price',
        'total_profit'
    ];

    public function goodsOrder(): belongsTo
    {
        return $this->belongsTo(GoodsOrder::class, 'order_id', 'id');
    }


    public function goods(): BelongsTo
    {
        return $this->belongsTo(Goods::class);
    }

    /**
     * 创建订单商品表
     * @param $user_id
     * @param $order
     * @param $goods
     * @return GoodsOrderDetail|\Illuminate\Database\Eloquent\Builder|Model|\LaravelIdea\Helper\App\Models\_IH_GoodsOrderDetail_QB
     */
    public static function store($user_id, $order, $goods)
    {
        return self::query()->create([
            'order_id' => $order->id,
            'order_sn' => $order->order_sn,
            'user_id' => $user_id,
            'category_id' => $goods->category_id,
            'goods_id' => $goods->id,
            'name' => $goods->name,
            'img' => $goods->img,
            'number' => $goods->number,
            'original_price' => $goods->original_price,
            'price' => $goods->price,
            'profit' => $goods->profit,
            'total_original_price' => bcmul($goods->original_price, $goods->number),
            'total_price' => bcmul($goods->price, $goods->number),
            'total_profit' => bcmul($goods->profit, $goods->number)
        ]);
    }

    public static function getOrderGoodsByOrderId($order_id)
    {
        $goods = self::query()->where('order_id', $order_id)->get();
        // 计算相关总价
        $order_total_original_price = $order_total_price = $order_total_profit = 0;
        foreach ($goods as $val){
            $order_total_original_price = bcadd($order_total_original_price, $val->total_original_price);
            $order_total_price = bcadd($order_total_price, $val->total_price);
            $order_total_profit = bcadd($order_total_profit, $val->total_profit);
        }

        $data['goods'] = $goods;
        $getOrderAmount = self::getOrderAmount($goods->toArray());
        $data['order_total_original_price'] = $getOrderAmount['order_total_original_price'];
        $data['order_total_price'] = $getOrderAmount['order_total_price'];
        $data['order_total_profit'] = $getOrderAmount['order_total_profit'];
        return $data;
    }

    public static function getOrderAmount($order_detail){
        $order_total_original_price = $order_total_price = $order_total_profit = 0;
        foreach ($order_detail as $val){
            $order_total_original_price = bcadd($order_total_original_price, $val['total_original_price']);
            $order_total_price = bcadd($order_total_price, $val['total_price']);
            $order_total_profit = bcadd($order_total_profit, $val['total_profit']);
        }
        $data['order_total_original_price'] = $order_total_original_price;
        $data['order_total_price'] = $order_total_price;
        $data['order_total_profit'] = $order_total_profit;
        return $data;
    }


}
